/*
 * @Author: your name
 * @Date: 2020-05-03 20:48:29
 * @LastEditTime: 2020-05-03 20:49:47
 * @LastEditors: Please set LastEditors
 * @Description: In User Settings Edit
 * @FilePath: \MWZ_MIS\src\lib\crypto.js
 */
import { enc, DES, mode as _mode, pad } from 'crypto-js';

function encryptByDESModeEBC (str, key) {
  // str为待加密字符串，key为密匙
  const keyHex = enc.Utf8.parse(key);
  const encrypted = DES.encrypt(str, keyHex, {
    mode: _mode.ECB,
    padding: pad.Pkcs7
  });
  return encrypted.ciphertext.toString();
}

function decryptByDESModeEBC (ciphertext, key) {
  // ciphertext为带解密字符串，key为密匙
  const keyHex = enc.Utf8.parse(key);
  const decrypted = DES.decrypt(
    {
      ciphertext: enc.Hex.parse(ciphertext)
    },
    keyHex,
    {
      mode: _mode.ECB,
      padding: pad.Pkcs7
    }
  );
  return decrypted.toString(enc.Utf8);
}

export const encrypt = encryptByDESModeEBC;
export const decrypt = decryptByDESModeEBC;
